package com.joker.dynamic.thread.pool.sdk.register.redis;

import com.joker.dynamic.thread.pool.sdk.domain.model.entity.ThreadPoolConfigEntity;
import com.joker.dynamic.thread.pool.sdk.domain.model.valobj.RegistryEnumVO;
import com.joker.dynamic.thread.pool.sdk.register.IRegistry;
import org.redisson.api.RBucket;
import org.redisson.api.RList;
import org.redisson.api.RedissonClient;

import javax.annotation.Resource;
import java.time.Duration;
import java.util.List;

/**
 * Created By guzq on 2024/10/19
 */
public class RedisRegistry implements IRegistry {


    private final RedissonClient redisClient;

    public RedisRegistry(RedissonClient redisClient) {
        this.redisClient = redisClient;
    }

    @Override
    public void reportThreadPool(List<ThreadPoolConfigEntity> theadPoolConfigEntities) {
        //上报到 redis
        RList<ThreadPoolConfigEntity> list = redisClient.getList(RegistryEnumVO.THEAD_POOL_CONFIG_LIST_KEY.getKey());
        list.delete();
        list.addAll(theadPoolConfigEntities);
    }

    @Override
    public void reportThreadPoolConfigParameter(ThreadPoolConfigEntity threadPoolConfigEntity) {
        String cacheKey = RegistryEnumVO.THREAD_POOL_CONFIG_PARAMETER_LIST_KEY.getKey() + threadPoolConfigEntity.getAppName() +  threadPoolConfigEntity.getThreadPoolName();
        RBucket<Object> bucket = redisClient.getBucket(cacheKey);
        bucket.set(threadPoolConfigEntity, Duration.ofDays(30));
    }
}
